package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 前端检查组接口
 * @author Administrator
 */
@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {
    /**
     * 远程调用接口
     */
    @Reference
    private CheckGroupService checkGroupService;
    /**
     *
     * @param checkGroup
     * 检查组
     * @return
     * 结果
     */
    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup,Integer[] checkItemIds){
        try {
            checkGroupService.add(checkGroup,checkItemIds);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }
    /**
     * @param pageBean 分页信息
     * @return         检查组信息
     */
    @RequestMapping("/findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean pageBean){
        PageResult byPage = checkGroupService.findByPage(pageBean);
        return byPage;
    }

    /**
     * 通过检查项id查询检查组
     * @param id 检查项id
     * @return   检查组
     */
    @RequestMapping("/findCheckGroupById")
    public Result findCheckGroupById(Integer id){
        try {
            CheckGroup checkGroup=checkGroupService.findCheckGroupById(id);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     * @param id 检查组id
     * @return   检查组id对应的所有检查项id
     */
    @RequestMapping("/findCheckItemsByCheckGroupId")
    public Result findCheckItemsByCheckGroupId(Integer id){
        try {
            List<Integer> checkItemIds=checkGroupService.findCheckItemsByCheckGroupId(id);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    /**
     * 修改检查组及检查组对应的检查项信息
     * @param checkItemIds 检查组对应的检查项信息
     * @param checkGroup   检查项
     * @return  **
     */
    @RequestMapping("/edit")
    public Result edit(Integer[] checkItemIds,@RequestBody CheckGroup checkGroup){
        try {
            checkGroupService.editCheckGroupAndCheckGroupReCheckitem(checkItemIds,checkGroup);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    /**
     * 通过检查组id删除检查组
     * @param id    检查组id
     * @return **
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            checkGroupService.deleteByCheckGroupId(id);
            return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<CheckGroup> checkGroups=checkGroupService.findAll();
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroups);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
}
